#include <cstdlib>
#include <cassert>

#include "AM_Rand.hpp"

namespace AutoMatic
{
	int getRand(void)
	{
		return rand(); // % 10 + 10;
	}

	void GenerateAndCheckRandomNumberList(int val_, bool srand_ = true)
	{
		static const int MAX_RANDOM_NUMBERS = 10000;
		static int randomNumbers[MAX_RANDOM_NUMBERS];

		// Set the random seed.
		// If srand_ is false, then this function will _probably_ assert.
		// There is no guarantee that the first five numbers for a certain seed 
		// aren't found somewhere in the list generated by another seed.
		// In fact, with large enough list, the probability would have to be nearly
		// 100%.
		if(srand_){ srand(val_); }
		
		for (int i = 0; i < MAX_RANDOM_NUMBERS; ++i)
		{
			randomNumbers[i] = getRand();
		}

		srand(val_);	// Reset the random seed.

		// Assert that the list of random numbers is the same as before
		for (int i = 0; i < MAX_RANDOM_NUMBERS; ++i)
		{
			assert(getRand() == randomNumbers[i]);
		}
		
	}

	void RunRandomSeedAndNumberGenerationTest(void)
	{
		static const int MAX_SEED_NUMBER = 1000;
		static const int MIN_SEED_NUMBER = 0;

		int dir = 1;
		int i = MIN_SEED_NUMBER;

		// GenerateAndCheckRandomNumberList(0, false); // Will assert

		while(i > MIN_SEED_NUMBER - 1)
		{ // For each of the seed numbers
			GenerateAndCheckRandomNumberList(i);
			i += dir;
			if(i > MAX_SEED_NUMBER){ dir = -1; }
		}
	}
}
